package com.hbm.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbm.eduservice.client.VodClient;
import com.hbm.eduservice.entity.*;
import com.hbm.eduservice.entity.frontvo.CourseFrontVo;
import com.hbm.eduservice.entity.frontvo.CourseWebVo;
import com.hbm.eduservice.entity.vo.CourseInfoVo;
import com.hbm.eduservice.entity.vo.CoursePublishVo;
import com.hbm.eduservice.mapper.EduCourseMapper;
import com.hbm.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbm.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-20
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private VodClient vodClient;

    //课程描述注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduSubjectService subjectService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;


    //添加课程基本信息的方法
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1 向课程表添加课程基本信息
        //CourseInfoVo对象转换eduCourse对象

        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert=baseMapper.insert(eduCourse);//返回的是影响的行数
        if(insert == 0) {
            //添加失败
            throw new GuliException(20001,"添加课程信息失败");
        }

        String cid = eduCourse.getId();

        //2 向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程id
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);

        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo=new CourseInfoVo();

        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);


        EduCourseDescription description = courseDescriptionService.getById(courseId);

        BeanUtils.copyProperties(description,courseInfoVo);



        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0) {
            throw new GuliException(20001,"修改课程信息失败");
        }

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        boolean b = courseDescriptionService.updateById(eduCourseDescription);
        if(b == false) {
            throw new GuliException(20001,"修改课程信息失败");
        }

    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);

        return publishCourseInfo;
    }

    @Override
    public void removeCourse(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        String id = eduCourse.getId();

        //根据课程id删视频
        QueryWrapper<EduVideo> aliyvideoWrapper = new QueryWrapper<>();
        aliyvideoWrapper.eq("course_id",courseId);
        aliyvideoWrapper.select("video_source_id");//查询指定字段
        List<EduVideo> eduVideoList = videoService.list(aliyvideoWrapper);
        //从 eduVideoList取值删除视频
        List<String> videoIds=new ArrayList<>();
        for (int i = 0; i < eduVideoList.size(); i++) {
            EduVideo eduVideo = eduVideoList.get(i);
            //String videoSourceId = eduVideoList.get(i).toString();//由于查询指定字段 查询出来的数据就是video_source_id
            String videoSourceId = eduVideo.getVideoSourceId();
            //System.out.println("=====================");
            //System.out.println("=====================");
            //System.out.println("videoSourceId:"+videoSourceId);
            if(!videoSourceId.isEmpty()){
                videoIds.add(videoSourceId);
            }
        }
        if (videoIds.size()>0){
            vodClient.deleteBatch(videoIds);
        }


        //删除小节
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",id);
        videoService.remove(videoWrapper);

        //删除章节
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id",id);
        chapterService.remove(chapterWrapper);

        //删除描述
        courseDescriptionService.removeById(id);

        //删除本身
        int i = baseMapper.deleteById(courseId);
        if(i==0){
            throw new GuliException(20001,"删除失败");
        }


    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        //2 根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageCourse,wrapper);
        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();//下一页
        boolean hasPrevious = pageCourse.hasPrevious();//上一页

        HashMap<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);


        return map;
    }

    //自定义sql
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
//        EduCourse eduCourse = baseMapper.selectById(courseId);
//        CourseWebVo courseWebVo = new CourseWebVo();
//        BeanUtils.copyProperties(eduCourse,courseWebVo);
//        return courseWebVo;
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
